package cn.itcast.erp.controller;

import cn.itcast.erp.annotation.CheckPower;
import cn.itcast.erp.entity.Emp;
import cn.itcast.erp.entity.Menu;
import cn.itcast.erp.entity.Tree;
import cn.itcast.erp.entity.utils.ResultAjax;
import cn.itcast.erp.service.MenuService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class MenuController {

    @Resource
    private MenuService ms;

    @RequestMapping("/menuTree")
    public List<Menu> menu(@SessionAttribute("emp") Emp emp){
        List<Menu> menu = ms.getMenu(emp.getUuid());
        return menu;
    }

    // 角色权限列表显示(最后一个)
    @CheckPower("角色权限设置")
    @RequestMapping("/readRoleMenus")
    public List<Tree> readRoleMenus(String id){

        return ms.readRoleMenus(Long.valueOf(id));
    }

    // 更改角色权限
    @CheckPower("角色权限设置")
    @RequestMapping("/updateRoleMenus")
    public ResultAjax updateEmpRoles(String id, String checkedStr){
        ResultAjax resultAjax = new ResultAjax();
        int i = ms.updateRoleMenus(Long.valueOf(id), checkedStr);
        if (i > 0){
            resultAjax.setMessage("修改成功!!");
        }else {
            resultAjax.setMessage("修改失败!!");
        }
        return resultAjax;

    }

}
